import org.molamil.acto.core.Logger;
import org.molamil.acto.pages.MovieBuilder;
import org.molamil.acto.pages.MovieContext;
import org.molamil.acto.pages.factory.MovieFactory;

/**
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.factory.BasicMovieFactory implements MovieFactory {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("BasicMovieFactory");
	
	private var _list:Array;
	
	
	// Constructor
	
	/**
	 * Creates an empty BasicMovieFactory.
	 */
	public function BasicMovieFactory(list:Array) {
		log.debug("Constructor");
		if (list != null) {
			_list = list;
		} else {
			_list = new Array();
		}
	}
	
	
	// Functions
	
	/**
	 * Checks that the movie exists.
	 * 
	 * @param   name Name of the movie.
	 * @return  Whether the movie exists or not.
	 */
	public function containsMovie(name:String):Boolean {
		var b:Boolean = getMovie(name) != null;
		log.debug("containsMovie(), name = " + name + ": " + b);
		return b;
	}
	
	/**
	 * Gets all movies.
	 *    
	 * @return  An array containing all movies as <code>MovieContext</code>
	 */
	public function getMovies():Array {
		log.info("getMovies(), returning " + _list.length);
		return _list;
	}
	
	/**
	 * Gets a movie by name.
	 *    
	 * @param   name Movie name.
	 * @return  If found, a <code>MovieContext</code> object 
	 * 			(<code>ExternalMovieMaster</code>, <code>InternalMovieMaster</code> or <code>LinkedMovieMaster</code>).	
	 * 			If not found, <code>null</code>.
	 */
	public function getMovie(name:String):MovieContext {
		
		log.info("getMovie(), name = " + name);
		
		var p:MovieContext = null;
		
		for (var i:Number = 0; i < _list.length; i++) {
			var pt:MovieContext = _list[i];
			if (pt.name == name) {
				p = pt;
				break;
			}
		}
		
		if (p == null)
			log.warning("getMovie(), name = " + name + " not found");
			
		return p;
		
	}
	
	public function getExternalMovie(name:String):MovieContext {
		log.info("getExternal(), name = " + name);
		var p:MovieContext = getMovie(name);
		if (p.type == MovieBuilder.TYPE_EXTERNAL)
			log.warning("getExternal(), name = " + name + " not found");
		return p;
	}
	
	public function getInternalMovie(name:String):MovieContext {
		log.info("getInternal(), name = " + name);
		var p:MovieContext = getMovie(name);
		if (p.type == MovieBuilder.TYPE_INTERNAL)
			log.warning("getInternal(), name = " + name + " not found");
		return p;
	}
	
	public function getLinkedMovie(name:String):MovieContext {
		log.info("getLinked(), name = " + name);
		var p:MovieContext = getMovie(name);
		if (p.type == MovieBuilder.TYPE_LINKED)
			log.warning("getLinked(), name = " + name + " not found");
		return p;
	}
	
	
}